#导入模块

import pygame
from pygame.locals import *
import time,random

class HeroPlane:
	'''玩家飞机'''
	def __init__(self,screen_temp):
		self.x = 150
		self.y = 400
		self.screen = screen_temp
		self.image = pygame.image.load("./pic/heroL.png")
		self.bullet_list=[] #玩家的子弹列表

	def display(self):

		#绘制子弹
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)

		'''绘制玩家飞机'''
		self.screen.blit(self.image,(self.x,self.y))
		
	def move_left(self):
		'''左移动飞机'''
		self.x -= 15

	def move_right(self):
		'''右侧飞机'''
		self.x +=15

	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))

class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):

		self.x = x+77
		self.y = y
		self.x1 = x+77
		self.y1 = y+130
		self.screen = screen_temp
		self.image = pygame.image.load("./pic/bullet1.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def display2(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x1,self.y1))


	def move(self):

		self.y -=10

		if self.y < -40:
			return True

	def move2(self):

		self.y += 50

		if self.y >600:
			
			return True


def key_control(hero_temp):
	'''键盘控制函数'''

	#执行程序的退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			print("exit()")
			exit()

	#获取按键信息
	pressed_keys = pygame.key.get_pressed()

	#做判断，并执行对象的操作
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print("Left...")
		hero_temp.move_left()

		if hero_temp.x<=0:
			hero_temp.x=0
	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print("Right...")
		hero_temp.move_right()

		if hero_temp.x >= 357:
			hero_temp.x=357

	if pressed_keys[K_SPACE]:
		print("space...")
		hero_temp.fire()

class enemyPalne:
	'''敌人飞机'''
	def __init__(self,screen_temp):

		self.x =  random.choice(range(400))
		self.y = -160
		self.screen = screen_temp
		self.image = pygame.image.load("./pic/enemyPlane"+str(random.choice(range(3)))+".png")
		self.bullet_list=[] #敌机的子弹列表
		

	def display(self):

		#绘制子弹
		for b in self.bullet_list:
			b.display2()
			if b.move2():
				self.bullet_list.remove(b)

		'''绘制敌机'''
		self.screen.blit(self.image,(self.x,self.y))

	def fire(self):

		self.bullet_list.append(Bullet(self.screen,self.x,self.y))
		#self.bullet_list = [Bullet(self.screen,self.x,self.y)]
	

	def move(self,hero):

		self.y += 5

		#移除子弹
		if self.y > 568:
			return True
		#遍历所有子弹，并执行碰撞检测
		for bo in hero.bullet_list:
			if bo.x > self.x +20 and bo.x < self.x + 130 and bo.y > self.y +20 and bo.y < self.y +100:
				hero.bullet_list.remove(bo)
				return True

'''
class explore:

	def __init__(self,screen_temp,x, y):
		self.x = x
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./pic/explore"+str(random.choice(range(3)))+".png")

	def display(self,m,n):
		self.screen.blit(self.image,(m,n))
'''

def main():
	#创建游戏窗口
	screen = pygame.display.set_mode((500,568),0,0)

	#创建玩家飞机
	hero = HeroPlane(screen)

	m = - 750#####################
	enemylist = [] #存放敌机列表
	enemyX = 0
	enemyY = 0

	heroX = 0
	heroY = 0

	while True:

		#创建一个游戏背景
		background = pygame.image.load("./pic/mapf.png")

		#绘制画面
		screen.blit(background,(0,m))
		m +=2

		if m >=- 70:
			m =- 750

		#绘制玩家飞机
		hero.display()

		#执行退出
		key_control(hero)

		#随机绘制敌机
		if random.choice(range(25)) ==10:
			enemylist.append(enemyPalne(screen))
			#Bullet.display(screen)

		#遍历敌机并绘制移动
		for em in enemylist:
			em.fire()
			em.display()
			
			if em.move(hero):
				enemyX = em.x
				enemyY = em.y
				enemylist.remove(em)

				image = pygame.image.load("./pic/explore"+str(random.choice(range(3)))+".png")
				screen.blit(image,(enemyX,enemyY))
		
		#更新显示
		pygame.display.update()

		#定时显示
		time.sleep(0.04)



#判断当前是否是主运行程序，并调用
if __name__ =="__main__":
	main()